var imgUrl = "/user/createCaptcha"; //验证码url
var flag = ["login", "register", "forget"]; //
var loginType = 0;

function setLoginType(login_Type) {
    loginType = login_Type;
}

function checkLoginBtn(obj) {
    var inputArr = obj.parent().parent().find("input");
    if (inputArr == null || inputArr.length == 0) {
        return;
    }
    var btn = obj.parent().siblings(".btn");
    var ff = 1;
    inputArr.each(function() {
        if (!!this.getAttribute("data-flag")) {
            ff = ff * (this.getAttribute("data-flag") * 1);
        }
    });

    if (ff == 1) {
        btn.css("background-color", "#4f6dcf");
        btn.css("cursor", "pointer");
    } else {
        btn.css("background-color", "rgb(169, 169, 169)");
        btn.css("cursor", "default");
    }
}

function changeCodePic(imgObj) {
    imgObj[0].onerror = function(e) {
        imgObj[0].src = "../../includes/images/reflash_code.png"/*tpa=http://www.quyundong.com/gosport/includes/images/reflash_code.png*/;
    }
    imgObj[0].src = imgUrl + "?" + new Date().getTime();

}

function createCaptcha(imgObj) {
    setTimeout(function() {
        changeCodePic(imgObj);
    }, 10);
}

var checkPhoneReqCallback = function(obj, datas) {
    var err_obj = obj.parent().find("div");
    var errorText;
    var btnGetCode = obj.parent().siblings(".code").find("button");
    var tag = obj.parent().parent().attr("data-tag");
    if (datas.status == "0000") { //未注册
        if (tag == flag[0]) { //login
            if (loginType == 0) {
                errorText = "手机未注册";
                showErrorTips(err_obj, errorText);
                obj.attr("data-flag", "0");
            } else {
                obj.attr("data-flag", "1");
                btnGetCode.css("background-color", "#4f6dcf");
                btnGetCode.css("cursor", "pointer");
            }
        } else if (tag == flag[1]) { // register
            hideErrorTips(err_obj);
            obj.attr("data-flag", "1");
            btnGetCode.css("background-color", "#4f6dcf");
            btnGetCode.css("cursor", "pointer");

        } else if (tag == flag[2]) { // forget
            errorText = "手机未注册";
            showErrorTips(err_obj, errorText);
            obj.attr("data-flag", "0");
        }

    } else if (datas.status == "0207") { //已注册

        if (tag == flag[0]) {
            obj.attr("data-flag", "1");
            btnGetCode.css("background-color", "#4f6dcf");
            btnGetCode.css("cursor", "pointer");
        } else if (tag == flag[1]) {
            errorText = "手机已注册";
            showErrorTips(err_obj, errorText);
            obj.attr("data-flag", "0");
        } else if (tag == flag[2]) {
            obj.attr("data-flag", "1");
            btnGetCode.css("background-color", "#4f6dcf");
            btnGetCode.css("cursor", "pointer");
        }
    }

    checkLoginBtn(obj);
}



function phoneInputListener(obj) {

    obj.on("input keyup", function() {



        var err_obj = obj.parent().find("div");
        var btnGetCode = obj.parent().siblings(".code").find("button");
        if (obj.prop("value").length > 0) {
            hideErrorTips(err_obj);
        }
        if (obj.prop("value").length == 11) {
            if (isPhoneNumber(obj.prop("value"))) {
                obj.attr("data-flag", "1");
                btnGetCode.css("background-color", "#4f6dcf");
                btnGetCode.css("cursor", "pointer");
            } else {
                showErrorTips(err_obj, "手机格式错误");
                btnGetCode.css("background-color", "rgb(169, 169, 169)");
                btnGetCode.css("cursor", "default");
                obj.attr("data-flag", "0");
            }
        } else {
            btnGetCode.css("background-color", "rgb(169, 169, 169)");
            btnGetCode.css("cursor", "default");
            obj.attr("data-flag", "0");
        }
        checkLoginBtn(obj);
    });
}

function checkPhone(obj) {

    if (obj.prop("value").length == 11 && isPhoneNumber(obj.prop("value"))) {
        return true;
    } else {
        var text;
        if (obj.prop("value").length == 0)
            text = "请填写手机号";
        else
            text = "手机格式错误";
        showErrorTips(obj.parent().find("div"), text);
        return false;
    }
}

function codeInputListener(obj) {
    obj.on("input keyup", function() {
        if ($(this).prop("value").length > 0) {
            hideErrorTips($(this).parent().find("div"));
            $(this).attr("data-flag", "1");
        } else {
            $(this).attr("data-flag", "0");
        }
        checkLoginBtn($(this));
    });
}

function checkCode(obj) {
    if (obj.prop("value").length > 0) {
        return true;
    } else {
        showErrorTips(obj.parent().find("div"), "请填写验证码");
        return false;
    }
}

function passwordInputListener(obj) {
    obj.on("input keyup", function() {
        if ($(this).prop("value").length >= 6) {
            hideErrorTips($(this).parent().find("div"));
            $(this).attr("data-flag", "1");
        } else {
            $(this).attr("data-flag", "0");
        }
        checkLoginBtn($(this));
    });
}

function checkPassword(obj) {

    if (obj.prop("value").length >= 6) {
        return true;
    } else {
        if (obj.prop("value").length > 0 && obj.prop("value").length < 6) {
            showErrorTips(obj.parent().find("div"), "密码不能少于6位");
        } else {
            showErrorTips(obj.parent().find("div"), "请填写密码");
        }
        return false;
    }
}

function setAnimate(objArray, dirDis, animateTime) {
    $.each(objArray, function(i, obj) {
        obj.animate(dirDis, animateTime);
    });
}
